Skip to content

test: add flag detection tests#182

Open
oritwoen wants to merge 1 commit intounjs:mainfrom
oritwoen:test/flags-detection
Open

test: add flag detection tests#182
oritwoen wants to merge 1 commit intounjs:mainfrom
oritwoen:test/flags-detection

Conversation

@oritwoen
Copy link

@oritwoen oritwoen commented Mar 11, 2026

Adds test coverage for flags.ts, which had no dedicated tests despite containing the most complex boolean logic in the library (isCI, isColorSupported, isMinimal).

29 tests covering every exported flag. The interesting ones are the isCI interaction tests - verifying that providerInfo.ci !== false works correctly (e.g., Vercel with ci: false keeps isCI false while GitHub Actions without explicit ci metadata makes it true). Also catches the NO_COLOR vs FORCE_COLOR precedence and MODE as a fallback for NODE_ENV.

Uses vi.resetModules() + dynamic import to re-evaluate the module-level constants with fresh env state per test, same pattern as agents.test.ts but adapted for constants instead of a detect*() function.

Partial progress on #62.

Summary by CodeRabbit

  • Tests
    • Added comprehensive test suite for environment and platform detection utilities.
    • Tests verify correct behavior across CI systems, deployment environments (development/production), debug modes, color output support, and platform identification.
    • Implements proper environment variable stubbing and module reloading to ensure test isolation and accurate validation.

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Walkthrough

A new Vitest test suite was added for the flags utility module, comprehensively testing environment-based flag detection logic across CI detection, runtime environment identification, color support, Node.js versioning, and platform detection with isolated test cases using dynamic imports and environment variable stubbing.

Changes

Cohort / File(s) Summary
Flags Utility Test Suite
test/flags.test.ts
Added 227 lines of tests covering isCI, isTest, isDevelopment, isProduction, isDebug, isMinimal, isColorSupported, nodeVersion, nodeMajorVersion, and platform flags. Uses dynamic imports with environment variable stubbing and per-test module reloading.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 The flags are tested, hooray, hooray!
Environment variables bend to our way,
CI, production, and colors so bright,
Each test shines clear in the developer's light!
The suite hops along with precision and care,
Making sure every flag finds truth over there! 🎉

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'test: add flag detection tests' directly and clearly summarizes the main change: adding a comprehensive test suite for flag detection in the flags utility module.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/flags.test.ts (1)

149-167: isMinimal still misses the !hasTTY path.

src/flags.ts computes isMinimal from four operands, but this block only exercises three of them. Adding one stdout.isTTY = false case, plus an all-false case, would close the remaining gap and catch regressions in the final branch.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/flags.test.ts` around lines 149 - 167, Add tests to cover the missing
branches for isMinimal: add a test that sets process.stdout.isTTY = false
(mocking/restoring it around the test) and asserts importFlags().isMinimal ===
true, and add an additional test where none of MINIMAL, CI, NODE_ENV are set and
stdout.isTTY is true (or reset to default) and assert importFlags().isMinimal
=== false; reference the importFlags function and the isMinimal export and
ensure the stdout.isTTY manipulation is done only inside the test to avoid
leaking state.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@test/flags.test.ts`:
- Around line 3-24: The test only clears a subset of env keys so detectProvider
(in src/providers.ts) can still see other provider-related variables and flip
behavior for isMinimal/isColorSupported; update the test setup in
test/flags.test.ts to either 1) neutralize every provider env name exported/used
by detectProvider by importing the provider key list from src/providers.ts and
clearing them from process.env before each test, or 2) replace process.env
entirely for the suite with an isolated copy (e.g., save original = process.env,
set process.env = {} or a controlled object, then restore original after tests)
so detectProvider, isMinimal and isColorSupported see a deterministic
environment. Ensure you reference detectProvider and the provider keys from
src/providers.ts so new provider env names are automatically handled.

---

Nitpick comments:
In `@test/flags.test.ts`:
- Around line 149-167: Add tests to cover the missing branches for isMinimal:
add a test that sets process.stdout.isTTY = false (mocking/restoring it around
the test) and asserts importFlags().isMinimal === true, and add an additional
test where none of MINIMAL, CI, NODE_ENV are set and stdout.isTTY is true (or
reset to default) and assert importFlags().isMinimal === false; reference the
importFlags function and the isMinimal export and ensure the stdout.isTTY
manipulation is done only inside the test to avoid leaking state.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1bd0f5fd-694f-4d52-a5eb-d5074afb6c2b

📥 Commits

Reviewing files that changed from the base of the PR and between 2b364bd and bef3c8f.

📒 Files selected for processing (1)
  • test/flags.test.ts

Comment on lines +3 to +24
const flagEnvKeys = [
"CI",
"NODE_ENV",
"MODE",
"TEST",
"DEBUG",
"MINIMAL",
"NO_COLOR",
"FORCE_COLOR",
"TERM",
"GITHUB_ACTIONS",
"VERCEL",
"VERCEL_ENV",
"NOW_BUILDER",
"GITLAB_CI",
"TRAVIS",
"CIRCLECI",
"BUILDKITE",
"APPVEYOR",
"CODESANDBOX_SSE",
"CODESANDBOX_HOST",
];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Isolate the full provider env surface in test setup.

detectProvider() scans many more env names than the subset cleared here. If these tests run on an unlisted CI provider, the leaked env will make the "no CI" cases fail and can also flip isMinimal / isColorSupported. Please either neutralize every provider key from src/providers.ts or replace process.env with an isolated test env for this suite.

Also applies to: 32-36

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/flags.test.ts` around lines 3 - 24, The test only clears a subset of env
keys so detectProvider (in src/providers.ts) can still see other
provider-related variables and flip behavior for isMinimal/isColorSupported;
update the test setup in test/flags.test.ts to either 1) neutralize every
provider env name exported/used by detectProvider by importing the provider key
list from src/providers.ts and clearing them from process.env before each test,
or 2) replace process.env entirely for the suite with an isolated copy (e.g.,
save original = process.env, set process.env = {} or a controlled object, then
restore original after tests) so detectProvider, isMinimal and isColorSupported
see a deterministic environment. Ensure you reference detectProvider and the
provider keys from src/providers.ts so new provider env names are automatically
handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant